﻿package frEngine.loaders.particleSub
{
    import flash.geom.*;
    import frEngine.loaders.particleSub.particleInstance.*;

    public class NodeEmitter extends EmitterObject
    {
        private var _areaValueList:Array;
        private var _maxY:Number = -100000000;
        private var _minY:Number = 100000000;
        private var _meshUrl:Object;

        public function NodeEmitter(param1:Object, param2:String, param3:String)
        {
            this._meshUrl = param1;
            super(param2, param3);
            if (this.meshData.hasLoaded)
            {
                this.initData();
            }
            else
            {
                initCompleted = false;
                this.meshData.callBack = this.initData;
            }
            return;
        }// end function

        override public function get meshData() : MeshBase
        {
            if (!_meshData && !hasDisposed)
            {
                _meshData = new MeshParticle(this._meshUrl, null);
            }
            return _meshData;
        }// end function

        private function initData() : void
        {
            posVector = _meshData.vertexVector;
            indexVector = _meshData.indexVector;
            sizePerVertex = _meshData.sizePerVertex;
            positionOffset = 0;
            uvPosVector = _meshData.vertexVector;
            uvSizePerVectex = _meshData.sizePerVertex;
            uvOffset = _meshData.uvOffset;
            checkHasInitCompleted();
            return;
        }// end function

        override public function dispose() : void
        {
            super.dispose();
            posVector = null;
            indexVector = null;
            return;
        }// end function

        override protected function processAxis(param1:int) : void
        {
            _vect3d.x = 0;
            _vect3d.z = 0;
            _vect3d.y = 0;
            return;
        }// end function

        override protected function processVolume(param1:int) : void
        {
            this.processCurve(param1);
            _vect3d.x = _vect3d.x * randnomArr0[param1];
            _vect3d.z = _vect3d.z * randnomArr1[param1];
            return;
        }// end function

        override protected function processEdage(param1:int) : void
        {
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_2:* = randnomArr0[param1];
            var _loc_3:* = randnomArr1[param1];
            var _loc_4:* = this.randomFaceIndex(param1, _loc_2);
            var _loc_5:* = _loc_2 * 3;
            if (_loc_5 < 1)
            {
                _loc_6 = indexVector[_loc_4 * 3] * sizePerVertex + positionOffset;
                _loc_7 = indexVector[_loc_4 * 3 + 1] * sizePerVertex + positionOffset;
            }
            else if (_loc_5 < 2)
            {
                _loc_6 = indexVector[_loc_4 * 3] * sizePerVertex + positionOffset;
                _loc_7 = indexVector[_loc_4 * 3 + 2] * sizePerVertex + positionOffset;
            }
            else
            {
                _loc_6 = indexVector[_loc_4 * 3 + 1] * sizePerVertex + positionOffset;
                _loc_7 = indexVector[_loc_4 * 3 + 2] * sizePerVertex + positionOffset;
            }
            pointA.x = posVector[_loc_6];
            pointA.y = posVector[(_loc_6 + 1)];
            pointA.z = posVector[_loc_6 + 2];
            pointB.x = posVector[_loc_7];
            pointB.y = posVector[(_loc_7 + 1)];
            pointB.z = posVector[_loc_7 + 2];
            pointB = pointB.subtract(pointA);
            _loc_5 = _loc_3;
            pointB.x = pointB.x * _loc_5;
            pointB.y = pointB.y * _loc_5;
            pointB.z = pointB.z * _loc_5;
            _vect3d = pointA.add(pointB);
            return;
        }// end function

        override protected function processVertex(param1:int) : void
        {
            var _loc_2:* = randnomArr0[param1];
            var _loc_3:* = posVector.length - positionOffset;
            _loc_3 = _loc_3 / sizePerVertex;
            param1 = int(_loc_3 * _loc_2) * sizePerVertex + positionOffset;
            _vect3d.x = posVector[param1];
            _vect3d.y = posVector[(param1 + 1)];
            _vect3d.z = posVector[param1 + 2];
            return;
        }// end function

        override protected function processCurve(param1:int) : void
        {
            var _loc_2:* = randnomArr0[param1];
            var _loc_3:* = randnomArr1[param1];
            var _loc_4:* = randnomArr2[param1];
            var _loc_5:* = this.randomFaceIndex(param1, _loc_2);
            this.findTargetFaceXYZ(_loc_5, _loc_2, _loc_3, _loc_4);
            return;
        }// end function

        private function randomFaceIndex(param1:int, param2:Number) : int
        {
            if (!this._areaValueList)
            {
                this.initArea();
            }
            var _loc_3:int = 0;
            var _loc_4:* = this._areaValueList.length;
            _loc_3 = param2 * _loc_4;
            while (_loc_3 >= _loc_4)
            {
                
                _loc_3 = _loc_3 - _loc_4;
            }
            return _loc_3;
        }// end function

        private function initMaxH() : void
        {
            var _loc_2:Number = NaN;
            if (this._maxY != -100000000)
            {
                return;
            }
            var _loc_1:* = posVector.length - positionOffset;
            var _loc_3:* = positionOffset;
            while (_loc_3 < _loc_1)
            {
                
                _loc_2 = posVector[(_loc_3 + 1)];
                this._maxY = _loc_2 > this._maxY ? (_loc_2) : (this._maxY);
                this._minY = _loc_2 < this._minY ? (_loc_2) : (this._minY);
                _loc_3 = _loc_3 + sizePerVertex;
            }
            return;
        }// end function

        private function initArea() : void
        {
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            this._areaValueList = new Array();
            var _loc_1:* = indexVector.length;
            var _loc_2:Number = 0;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1)
            {
                
                _loc_5 = indexVector[_loc_3] * sizePerVertex + positionOffset;
                _loc_6 = indexVector[(_loc_3 + 1)] * sizePerVertex + positionOffset;
                _loc_7 = indexVector[_loc_3 + 2] * sizePerVertex + positionOffset;
                pointA.x = posVector[_loc_5];
                pointA.y = posVector[(_loc_5 + 1)];
                pointA.z = posVector[_loc_5 + 2];
                pointB.x = posVector[_loc_6];
                pointB.y = posVector[(_loc_6 + 1)];
                pointB.z = posVector[_loc_6 + 2];
                pointC.x = posVector[_loc_7];
                pointC.y = posVector[(_loc_7 + 1)];
                pointC.z = posVector[_loc_7 + 2];
                pointA = pointA.subtract(pointC);
                pointB = pointB.subtract(pointC);
                pointC = pointA.crossProduct(pointB);
                _loc_8 = pointC.length / 2;
                this._areaValueList.push(_loc_8);
                _loc_2 = _loc_2 + _loc_8;
                _loc_3 = _loc_3 + 3;
            }
            _loc_1 = this._areaValueList.length;
            var _loc_4:Number = 0;
            _loc_3 = 0;
            while (_loc_3 < _loc_1)
            {
                
                _loc_9 = this._areaValueList[_loc_3] / _loc_2;
                _loc_4 = _loc_9 + _loc_4;
                this._areaValueList[_loc_3] = _loc_4;
                _loc_3++;
            }
            return;
        }// end function

        private function findTargetFaceXYZ(param1:int, param2:Number, param3:Number, param4:Number) : void
        {
            var _loc_5:* = indexVector[param1 * 3] * sizePerVertex + positionOffset;
            var _loc_6:* = indexVector[param1 * 3 + 1] * sizePerVertex + positionOffset;
            var _loc_7:* = indexVector[param1 * 3 + 2] * sizePerVertex + positionOffset;
            pointA.x = posVector[_loc_5];
            pointA.y = posVector[(_loc_5 + 1)];
            pointA.z = posVector[_loc_5 + 2];
            pointB.x = posVector[_loc_6];
            pointB.y = posVector[(_loc_6 + 1)];
            pointB.z = posVector[_loc_6 + 2];
            pointC.x = posVector[_loc_7];
            pointC.y = posVector[(_loc_7 + 1)];
            pointC.z = posVector[_loc_7 + 2];
            var _loc_8:* = pointC.subtract(pointA);
            var _loc_9:* = _loc_8.normalize() * param2;
            _loc_8.scaleBy(_loc_9);
            pointC = pointA.add(_loc_8);
            _loc_8 = pointB.subtract(pointA);
            _loc_9 = _loc_8.normalize() * param3;
            _loc_8.scaleBy(_loc_9);
            pointB = pointA.add(_loc_8);
            _loc_8 = pointC.subtract(pointB);
            _loc_9 = _loc_8.normalize() * param4;
            _loc_8.scaleBy(_loc_9);
            _loc_8 = pointB.add(_loc_8);
            _vect3d.x = _loc_8.x;
            _vect3d.y = _loc_8.y;
            _vect3d.z = _loc_8.z;
            return;
        }// end function

    }
}
